﻿//=============================================================
// Copyright (C) 2011 iSun Studio 软件工作室
// 作 者：江怀玉
// 日 期：2012-10-11
// 描 述：页面基类，所有的aspx页面继承自基类
//        页面基类提供常用的方法和常用的用户信息
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00   2011/10/11   江怀玉           
//=============================================================

using System.Collections.Generic;
using System.Linq;
using System.Text;
using iSun.Model;
using iSun.Entity;
using iSun.BLL.Base;

namespace iSun.Web.UI.Controls
{
    public class DojoListPage : ListPage
    {

        #region Query Parameter

        /// <summary>
        /// 按钮数组
        /// </summary>
        public string strButtons
        {
            get
            {
               return GetButtons();
            }
        }
        #endregion


        /// <summary>
        /// 获取Dojo数据段字
        /// </summary>
        public string DojoDataFields
        {
            get
            {
                return GetDojoDataFields();
            }
        }


        protected override void OnLoad(System.EventArgs e)
        {
            iSun.BLL.Base.BaseBussnies bll = new BLL.Base.BaseBussnies();
            var list = bll.GetEntitysByCondition<iSun.Model.Entity>("EntityName='" + this.EntityName + "'");
            iSun.Model.Entity entity = list != null ? list.FirstOrDefault() : null;
            if (entity != null)
            {
                _width = entity.EditPageWidth;
                _height = entity.EditPageHeight;
            }
            base.OnLoad(e);
        }


  

        /// <summary>
        /// 获取快速搜索的字段
        /// </summary>
        public string QuciklyQueryFields
        {
            get
            {
                return GetDataQueryFields();
            }
        }

        /// <summary>
        /// 获取数据搜索字段
        /// </summary>
        /// <returns></returns>
        private string GetDataQueryFields()
        {
            if (list == null)
            {
                DataListTemplate template = DataListTemplate.CreateInstance();
                BaseBussnies<DataListTemplate> bll = new BaseBussnies<DataListTemplate>(template);
                list = bll.GetEntityListByCondition<DataListTemplate>(" EntityName='" + EntityName + "'");
            }
            if (list == null)
            {
                return "";
            }
            var _list = list.Where(p => p.IsQuery == "Y");
            string _field= "";
            foreach (var item in _list)
            {
                _field = _field + item.Field+",";
            }
            return _field == string.Empty ? "" : _field.Remove(_field.Length - 1, 1);
        }


        IList<DataListTemplate> list;
        public override string GetDataFields()
        {
            return GetDojoDataFields();
        }
        /// <summary>
        /// 获取DOJO的数据列
        /// </summary>
        /// <returns></returns>
        public string GetDojoDataFields()
        {
            if (list == null)
            {
                DataListTemplate template = DataListTemplate.CreateInstance();
                BaseBussnies<DataListTemplate> bll = new BaseBussnies<DataListTemplate>(template);
                list = bll.GetEntityListByCondition<DataListTemplate>(" EntityName='" + EntityName + "'");
            }
            string _strHtml = "field:'{0}',name:'{1}',width:'{2}px',{3}";
            if (list == null)
            {
                return ("{" + string.Format(_strHtml, "Not Data", "Not Data", 400, "formatter:\"\"") + "},");
            }
            StringBuilder _sbHtml = new StringBuilder("");
            var _list = list.Where(p => p.IsVisiable == "Y" && p.Field!=this.EntityId).OrderBy(p => p.iOrder).ToList();
            int i = 0;
            foreach (DataListTemplate _template in _list)
            {
                i = i + 1;
                {
                    _sbHtml.Append("{" + string.Format(_strHtml, _template.Field, _template.THCaption,
                     (_template.Width > 0 ? _template.Width : 200)
                     , string.IsNullOrEmpty(_template.DataFormatter) ? "sortable:true,resizeable:true" : "formatter:" + (_template.DataFormatter + ",sortable:true,resizeable:true")) + "},");
                }
            }
            _sbHtml.Remove(_sbHtml.Length - 1, 1);
            return (_sbHtml.ToString());
        }





        /// <summary>
        /// 按钮组
        /// </summary>
        public string BtnItems
        {
            get;
            set;
        }



        /// <summary>
        /// 设置按钮操作权限
        /// </summary>
        public string GetButtons()
        {
            string functionid = GetStringFromQueryString("functionid", "");
            V_FunctionRight function = (new BaseBussnies(new V_FunctionRight())).GetEntityByCondition("FunctionID='" + functionid + "'") as V_FunctionRight;
            string userID = this.UserId.ToString();
            if (function == null)
            {
                BtnItems = "";
                BtnEvents = "";
                InitBtnEvents = "";
                return "";
            }
            else
            {
                string[] btns = function.Opration.Split(',');
                string[] btnTexts =  function.BarItems.Split(',');
                string[] fns    = function.FnItems.Split(',');
                string[] barItems = (new BLL.BUser()).GetMenuOperation(this.UserId.ToString(),this.FunctionID.ToString())[0].Split(',').Distinct().ToArray();
                if (barItems == null)
                {
                    return "";
                }
                if (barItems.Count() == 0)
                {
                    return "";
                }

                StringBuilder _buttons = new StringBuilder("");
                for (int i = 0; i < btns.Length; i++)
                {
                    if (barItems.Contains(btns[i])||this.UserName.ToLower()=="admin")
                    {
                        if (_buttons.ToString().Length == 0)
                        {
                            _buttons.Append(string.Format("\"{0}/{1}\"", fns[i], btnTexts[i]));
                        }
                        else
                        {
                            _buttons.Append(string.Format(",\"{0}/{1}\"", fns[i], btnTexts[i]));
                        }
                        // btns[i], btnTexts[i]));
                        // btns[i], fns[i]));
                    }
                }
                return _buttons.ToString();
                //BtnItems = strHTML.ToString();
                //BtnEvents = strHTML2.ToString();
                //InitBtnEvents = strHTML3.ToString();
            }
        }



    }

    
}
